package org.traffometer.traffosim;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * 
 * The View for this application, which is a Swing JFrame containing buttons and pull-down menus
 * with which to manipulate the Traffosim state, and a display of the Traffosim contents.
 * 
 * @author Joshua Stupplebeen
 *
 */
public class View implements Observer {

  /** The JFrame containing the GUI window. */
  private final JFrame frame = new JFrame("TraffoSim v2.0");
  /** Browse button. */
  private final JButton browseButton = new JButton("Browse");
  /** Run button. */
  private final JButton runButton = new JButton("Run");
  /** Clear button. */
  private final JButton clearButton = new JButton("Clear");
  /** Configuration file name gets entered here. */
  private final JTextField configFileField = 
  new JTextField("C:\\ICS413\\Traffometer-Sim-Aqua\\lib\\config\\static-sample.cfg");
  /** Output from packet generation. */
  private final JTextArea resultField = new JTextArea();;
  /** Label indicating config file path goes in this textfield. */
  private final JLabel jLabel1 = new JLabel("File:");
  /** Prefix for returned packet output. */
  private static final String simState = "Traffosim 2.0: ";

  /**
   * The required method to implement as part of the "Observer" contract. The controller will add an
   * instance of this View as an Observer to the Model, which will result in this method being
   * invoked whenever the Model's state changes.
   * 
   * @param obs The observable instance that invoked this.
   * @param arg The value it passed.
   */  
  public void update(Observable obs, Object arg) {
    this.resultField.setText(simState + arg.toString());
  }

  /**
   * Returns the JFrame associated with this View.  Used for Unit Testing.
   * @return The JFrame associated with the View. 
   */
  public JFrame getJFrame() {
    return this.frame;
  }
  
  /**
   * Returns the JTextField associated with this View.
   * @return The JTextField associated with the View.
   */
  public JTextArea getResultArea() {
    return this.resultField;
  }

  /**
   * Returns the JLabel associated with this View.
   * @return The JLabel associated with the View.
   */
  public JLabel getJLabel() {
    return this.jLabel1;
  }

  /**
   * Constructs the View. At the moment, does not actually do anything.
   */
  public View() {
    // does nothing.
  }

  /**
   * Constructs and displays the GUI on the AWT event thread to ensure thread-safety.
   */
  public void showGUI() {
    // Schedule a job for the event-dispatching thread:
    // creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        initGUI();
      }
    });
  }
  
  /**
   * Provides the gui initialization.
   *
   */
  private void initGUI() {
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    FlowLayout frameLayout = new FlowLayout();
    frame.setSize(238, 390);
    frame.getContentPane().setLayout(frameLayout);
    frame.getContentPane().add(jLabel1);
    frame.getContentPane().add(configFileField);
    configFileField.setPreferredSize(new java.awt.Dimension(175, 20));
    frame.getContentPane().add(browseButton);
    frame.getContentPane().add(runButton);
    frame.getContentPane().add(clearButton);
    frame.getContentPane().add(resultField);
    resultField.setPreferredSize(new java.awt.Dimension(700, 300));
    frame.pack();
    frame.setVisible(true);
  }

  /**
   * Enables the controller to add a listener to respond when the browse button is pushed.
   * 
   * @param listener The listener (supplied by the controller).
   */
  public void addBrowseListener(ActionListener listener) {
    this.browseButton.addActionListener(listener);
  }

  /**
   * Enables the controller to add a listener to respond when the run button is pushed.
   * 
   * @param listener The listener (supplied by the controller).
   */
  public void addRunListener(ActionListener listener) {
    this.runButton.addActionListener(listener);
  }

  /**
   * Enables the controller to add a listener to respond when the clear button is pushed.
   * 
   * @param listener The listener (supplied by the controller).
   */
  public void addClearListener(ActionListener listener) {
    this.clearButton.addActionListener(listener);
  }

}